package com.hnas.common.util;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSONObject;
import com.hnas.api.domain.RouteFinalValue;
import com.hnas.api.dto.DTOApiResponse;
import com.hnas.api.dto.DTOResult;
import com.hnas.api.server.PostConnServer;
import com.hnas.common.outdto.CardInfoForm;
import com.hnas.common.outdto.GetBalanceForm;
import com.hnas.core.util.StringUtil;

@Component("postConnServerUtils")
public class PostConnServerUtils {

	private static Log log = LogFactory.getLog(PostConnServerUtils.class);

	@Autowired
	private PostConnServer postConnServer;

	public String getBalance(GetBalanceForm getBalanceForm) throws Exception {
		List<JSONObject> list = postConnServer.getListResultData(getBalanceForm, RouteFinalValue.APP_GetAccBalanceByCardNo, JSONObject.class);
		JSONObject jsonObject = null;
		if(null != list && list.size() > 0) {
			jsonObject = list.get(0);
			if(StringUtil.isEmpty((String) jsonObject.get("balance"))) {
				throw new Exception("查询电子卡余额查询为空");
			}
//			return StringUtil.isEmpty((String) jsonObject.get("balance")) ? "0" : (String) jsonObject.get("balance");
			return (String) jsonObject.get("balance");
		} else {
			 throw new Exception("查询电子卡余额异常");
		}
		//return StringUtil.isEmpty((String) jsonObject.get("BALANCE")) ? "0" : (String) jsonObject.get("BALANCE");
	}

	public JSONObject getCardInfo(String cardNo) {
		CardInfoForm form = new CardInfoForm();
		form.setCardNo(cardNo);
		return postConnServer.getResultData(form, RouteFinalValue.APP_GetCardInfoByCardNo, JSONObject.class);
	}

	// 短信发送-不加密
	public JSONObject sendSMS(Object obj) {
		JSONObject res = new JSONObject();
		try {
			DTOApiResponse dtoResponse = postConnServer.getRequestData(obj, RouteFinalValue.APP_SmsSendNoPass, false);
			String resultcode = null;
			if (null != dtoResponse) {
				DTOResult dtoResult = dtoResponse.getResult();
				resultcode = dtoResult.getResultCode();
			}

			if (null != resultcode) {
				if (ResultCode.SUCCESS.equals(resultcode)) {
					res.put("result", true);
					res.put("msg", "短信发送成功");
					log.info(getcurrentTime() + "短信发送成功");
				} else {
					res.put("result", false);
					res.put("msg", "短信发送失败");
					log.info(getcurrentTime() + "短信发送失败");
				}
			} else {
				res.put("result", false);
				res.put("msg", "短信发送失败，服务异常");
				log.info(getcurrentTime() + "短信发送失败,服务异常");
			}
			
		} catch (Exception e) {
			e.printStackTrace();
			res.put("result", false);
			res.put("msg", "调用短信发送接口失败," + e.getMessage());
			log.error(getcurrentTime() + "调用短信发送接口失败");
		}
		return res;
	}

	public String getcurrentTime() {
		Date date = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String time = sdf.format(date);
		return time;
	}
}
